package com.css.zfzx.fxpg.modules.surveyspot.repository.impl;

import com.css.zfzx.fxpg.modules.surveyspot.repository.FxSurveySpotNativeRepository;
import com.css.zfzx.fxpg.modules.surveyspot.repository.entity.FxSurveySpotEntity;
import com.css.zfzx.fxpg.modules.surveyspot.viewobjects.FxSurveySpotQueryParams;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author leon
 * @date 2020-11-27
 */
@Repository
public class FxSurveySpotNativeRepositoryImpl implements FxSurveySpotNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(FxSurveySpotNativeRepositoryImpl.class);


    @Override
    public Page<FxSurveySpotEntity> queryFxSurveySpots(FxSurveySpotQueryParams queryParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from fx_survey_spot ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            whereSql.append(" and province = :province");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCityCountyName())) {
            whereSql.append(" and city_county_name = :cityCountyName");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getSurveySpotName())) {
            whereSql.append(" and survey_spot_name = :surveySpotName");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxSurveySpotEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from fx_survey_spot ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            query.setParameter("province", queryParams.getProvince());
            countQuery.setParameter("province", queryParams.getProvince());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCityCountyName())) {
            query.setParameter("cityCountyName", queryParams.getCityCountyName());
            countQuery.setParameter("cityCountyName", queryParams.getCityCountyName());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getSurveySpotName())) {
            query.setParameter("surveySpotName", queryParams.getSurveySpotName());
            countQuery.setParameter("surveySpotName", queryParams.getSurveySpotName());
        }
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<FxSurveySpotEntity> list = query.getResultList();
        BigInteger count = (BigInteger) countQuery.getSingleResult();
        Page<FxSurveySpotEntity> fxSurveySpotPage = new PageImpl<>(list, pageable, count.longValue());
        return fxSurveySpotPage;
    }

    @Override
    public List<FxSurveySpotEntity> listFxSurveySpots(FxSurveySpotQueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select * from fx_survey_spot ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            whereSql.append(" and province = :province");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCityCountyName())) {
            whereSql.append(" and city_county_name = :cityCountyName");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getSurveySpotName())) {
            whereSql.append(" and survey_spot_name = :surveySpotName");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxSurveySpotEntity.class);
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            query.setParameter("province", queryParams.getProvince());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCityCountyName())) {
            query.setParameter("cityCountyName", queryParams.getCityCountyName());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getSurveySpotName())) {
            query.setParameter("surveySpotName", queryParams.getSurveySpotName());
        }
        return query.getResultList();
    }

    @Override
    public List<FxSurveySpotEntity> listFxSurveySpotsByYears(FxSurveySpotQueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select * from fx_survey_spot ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if(!PlatformObjectUtils.isEmpty(queryParams.getYearStart())) {
            whereSql.append(" and year >= :yearStart");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getYearEnd())) {
            whereSql.append(" and year <= :yearEnd");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxSurveySpotEntity.class);
        if(!PlatformObjectUtils.isEmpty(queryParams.getYearStart())) {
            query.setParameter("yearStart", queryParams.getYearStart());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getYearEnd())) {
            query.setParameter("yearEnd", queryParams.getYearEnd());
        }
        return query.getResultList();
    }
}
